/* Andreas Baum
 * IMClientApp.java
 * Handles the connection of an IM client to the IM server
 * CS 78 - Networks - Programming Assignment 1
 */


package jbpiTunesProxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import jbpClientTester.DataThread;
import jbpClientTester.ServiceRegistrar;

import com.apple.dnssd.DNSSDException;



public class JBPiTunesProxy {

	public static int ITUNES_PORT = 3689;

	private static Socket iTunesSocket;
	private static Socket myiTunesSocket;
	private static ServerSocket serviceSocket;
	private static DataThread serviceThread;
	private static DataThread iTunesDataThread;
	private static DataThread myiTunesThread;

	private static String host;

	public JBPiTunesProxy() {
	}

	public static void main(String args[]) throws IOException {

		BufferedReader keyboardIn =
			new BufferedReader(
					new InputStreamReader( System.in ) );

		ServiceRegistrar iTunesRegistrar;

		System.out.println("\t*** Welcome to the Simple Bonjour Proxy! ***");		

		System.out.print("Please enter the address of the host >> ");
		host = keyboardIn.readLine();


		System.out.println("Attempting to connect to " + host + " on port " + ITUNES_PORT + "...");
		try {
			// send connection request:
			iTunesSocket = new Socket(host, ITUNES_PORT);
		} catch (UnknownHostException e) {
			// If host can't be found:
			System.err.println("ERROR: Don't know about host: " + host + ".");
			System.exit(1);
		} catch (IOException e) {
			// If host doesn't accept connections:
			System.err.println("ERROR: Couldn't get I/O for "
					+ "the connection to: " + host + ".");
			System.exit(1);
		}

		// Confirm connection:
		System.out.println("Connected to " + host 
				+ " on port " + ITUNES_PORT + ".");


		// connection is established, hand it over to a client thread.
		iTunesDataThread = new DataThread(iTunesSocket, "iTunesDataThread");

//		-------------------------------------------------------------------------

/*		System.out.println("Attempting to connect to localhost on iTunes port " + ITUNES_PORT + "...");
		try {
			// send connection request:
			myiTunesSocket = new Socket("localhost", ITUNES_PORT);
		} catch (UnknownHostException e) {
			// If host can't be found:
			System.err.println("ERROR: Don't know about localhost.");
			System.exit(1);
		} catch (IOException e) {
			// If host doesn't accept connections:
			System.err.println("ERROR: Couldn't get I/O for "
					+ "the connection to localhost.");
			System.exit(1);
		}

		// Confirm connection:
		System.out.println("Connected to localhost on iTunes port " 
				+ ITUNES_PORT + ".");


		// connection is established, hand it over to a client thread.
		myiTunesThread = new DataThread(myiTunesSocket, "myiTunesThread");
*/		

		

//		-------------------------------------------------------------------------

//		Register iTunes service listener, and establish connection to iTunes:
		System.out.println("Registering iTunes service.");
		String serviceName = "_daap._tcp";
		String otherName = "CLICK ME!! (JBP CLIENT)";

		// the port is allocated by the system:
		serviceSocket = new ServerSocket(0);

		try
		{
			// Register the daap service:
			iTunesRegistrar = new ServiceRegistrar(otherName, 
					serviceName, serviceSocket.getLocalPort());
			while (!iTunesRegistrar.isRegistered());
			// start a new thread on the socket as soon as iTunes sends
			// something back:
			serviceThread = new DataThread(serviceSocket.accept(), "iTunesServiceThread");
			serviceThread.setOtherThread(iTunesDataThread);
			iTunesDataThread.setOtherThread(serviceThread);
		} catch (InterruptedException e) {
			System.err.println("InterruptedException: " + e);
		}
		catch (DNSSDException e){
			System.err.println("DNSSDException: " + e);
		}
		catch (IOException e) {
			System.out.println("Accept failed: " + serviceSocket.getLocalPort());
			System.exit(-1);
		}
		
//		-------------------------------------------------------------------------
		
		System.out.println("Ready.");
		
//		-------------------------------------------------------------------------
/*		try {
 			System.out.print("> ");
			String msg = keyboardIn.readLine();
			iTunesDataThread.setOtherThread(myiTunesThread);
			myiTunesThread.setOtherThread(iTunesDataThread);
		} catch (IOException e) {
			e.printStackTrace();
		}*/
	}

}